package com.lgit.Model4.hw01;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class StudentDao {

    private List<Student> list;
    private static final String FILE_PATH = "src/com/lgit/Model4/file/Student.properties";
    private ObjectInputStream ois;
    private ObjectOutputStream oos;

    // 初始化学生信息
    public StudentDao() {
        File file = new File(FILE_PATH);
        /*ObjectOutputStream oos = null;
        ObjectInputStream ois = null;*/
        // 判断是否为第一次执行，如果是则添加如下的初始化数据
        if (0 == file.length()) {
            list = new ArrayList<>();
            list.add(new Student(1001, "刘备", 45));
            list.add(new Student(1002, "关羽", 40));
            list.add(new Student(1003, "张飞", 35));
            writeObject();
        } else {
            // 否则将数据写进文件中
            readObject();
        }
    }

    // 判断学号是否重复，如果重复则不能添加
    public int chargeStu(int id) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            if (id == list.get(i).getId()) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void writeObject() {
        OutputStream out = null;
        try {
            out = new FileOutputStream(FILE_PATH);
            oos = new ObjectOutputStream(out);
            // 将list集合中的数据写入到file文件中
            oos.writeObject(list);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public void readObject() {
        InputStream ins = null;
        try {
            ins = new FileInputStream(FILE_PATH);
            ois = new ObjectInputStream(ins);
            // 将从file中读取的数据转换为(list)类型并赋值给list
            list = (List<Student>)ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != ins) {
                try {
                    ins.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != ois) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 添加学生信息
    public int addStu(Student stu) {
        int status = 0;
        if (chargeStu(stu.getId()) == -1) { // 说明录入的学号不存在，可以录入学生信息
            list.add(stu);
            status = 1;
        }
        writeObject();
        return status;
    }

    // 删除学生信息
    public int removeStu(int id) { // 根据学号删除学生信息
        int status = 0;
        int index = chargeStu(id); // 判断学号是否存在，如果存在则执行删除操作
        if (-1 != index) {
            list.remove(index);
            status = 1;
        }
        writeObject();
        return status;
    }

    // 修改学生信息
    public int updateStu(int id, String name) { // 根据学号修改姓名
        int status = 0;
        int index = chargeStu(id);
        if (-1 != index) {
            Student stu = list.get(index); // 根据学号得到下标，根据下标获取学生信息
            stu.setName(name);
            status = 1;
        }
        writeObject();
        return status;
    }

    // 查询学生信息
    public void show() { // 遍历集合
        for (Student stu : list) {
            System.out.println(stu);
        }
    }
}
